﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using OCR.Function;
using OCR.SystemThread;
using OCR.Config;
using System.Globalization;
using MvCameraControl;

namespace OCR.SystemThread
{
    class CameraRestartThread
    {
        public static bool CameraRestartThread_Flowrec = false;
        public static Thread CameraRestartFlow_Thread = null;
        public static int iCameraRestart_Step = 0;
        

        public static bool CameraRestartThread_Pause = false;
        public static void CheckCameraRestart_Thread()
        {
            CameraRestartThread_Flowrec = true;
            CameraRestartFlow_Thread = new Thread(CameraRestartThreadFlow);
            CameraRestartFlow_Thread.IsBackground = true;
            CameraRestartFlow_Thread.Start();
        }

        private static void CameraRestartThreadFlow()
        {
            while (CameraRestartThread_Flowrec)
            {
                switch (iCameraRestart_Step)
                {
                    case 5:
                        CameraReceiveThread.RefreshDeviceListNoOpen();
                        if (CameraReceiveThread.device == null)
                        {
                            Console.WriteLine("-----无相机设备1-----");
                            OCR_Form.AppendLog("尝试重连..." + DateTime.Now.ToString("yyyy-MM-dd-HH:mm:ss"), isError: true);
                            iCameraRestart_Step = 10;
                        }
                        else
                        {
                            Console.WriteLine("CameraReceiveThread.device 不为空");
                            CameraOnlineThread.iCameraOnline_Step = 5;
                        }

                        break;
                    case 10:
                        CameraReceiveThread.RefreshDeviceListNoOpen();
                        if (CameraReceiveThread.device == null)
                        {
                            Console.WriteLine("-----无相机设备2-----");
                            OCR_Form.AppendLog("尝试重连..." + DateTime.Now.ToString("yyyy-MM-dd-HH:mm:ss"), isError: true);
                            iCameraRestart_Step = 15;
                        }
                        else
                        {
                            Console.WriteLine("CameraReceiveThread.device 不为空");
                            CameraOnlineThread.iCameraOnline_Step = 5;
                        }
                        break;
                    case 15:
                        if (!File.Exists("CameraStateResult.txt"))
                        {
                            File.WriteAllText("CameraStateResult.txt", null, Encoding.UTF8);
                        }
                        StreamWriter sw = File.AppendText("CameraStateResult.txt");
                        sw.WriteLine(LocalInfo.CameraStateResult);
                        sw.Close();

                        OCR_Form.oCR_Form.StopCameraTriggerThread();
                        iCameraRestart_Step = 0;

                        OCR_Form.oCR_Form.ShowCameraOfflineForm();
                        break;
                }

            }

        }
    }
}
